import {messages} from "share/common";
import React from 'react'
import {connect} from 'react-redux'
import { message, Popover } from "antd";
import interfaceService from "containers/financial-account/interface-definition/interface-service";
import ResizeTable from 'components/resize-table'
import ViewInfo from 'containers/financial-account/template/view-info';

class BasisInfo extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      interfaceRecord: props.recordInfo,
      page: 0,
      loading: false,
      data: [],
      pagination: {
        total: 0
      },
      columns: [
        {title: messages('financial-account.key166')/*编码*/, dataIndex: 'code'},
        {title: messages('financial-account.key167')/*名称*/, dataIndex: 'name'},
        {title: messages('financial-account.key3')/*说明*/, dataIndex: 'description', width: '30%', render: value => <Popover content={value} placement={'topLeft'}>{value}</Popover>},
        {title: messages('common.operation')/*操作*/, dataIndex: 'operation', width: 80, render: (value,record) =>
            <a onClick={() => this.handleView(record)}>{messages('common.view')/*查看*/}</a>}
      ],
      visible: false,
      title: '',
      code: ''
    }
  }

  componentWillMount() {
    this.getList();
  }

  handleView = record => {
    this.setState({
      visible: true,
      code: record.code,
      title: record.name
    })
  };

  getList = () => {
    const { page } = this.state;
    let temp = { page };
    this.setState({loading: true});
    interfaceService.getBasisList(temp).then(res => {
      this.setState({
        loading: false,
        data: res.data.rows,
        pagination: {
          total: Number(res.data.total),
          current: page + 1,
          onChange: this.onChangePager,
        }
      });
    }).catch(e => {
      this.setState({loading: false});
      message.error(e.response.data.message);
    })
  };

  onChangePager = (page) => {
    if (page - 1 !== this.state.page)
      this.setState({
        page: page - 1
      }, () => {
        this.getList();
      })
  };

  render(){
    const { columns, loading, data, pagination, visible, title, code } = this.state;
    return(
      <div>
        <ResizeTable
          columns={columns}
          loading={loading}
          bordered
          dataSource={data}
          pagination={pagination}
          rowKey={'id'}
        />
        <ViewInfo visible={visible} code={code} close={() => this.setState({visible: false})} title={title}/>
      </div>
    )
  }
}

function mapStateToProps(state){
  return {

  }
}

export default connect(mapStateToProps)(BasisInfo);
